#include "GRO_Checkbox.h"


// Every child of Widget should use these in draw()
#define DRAW_PAINTER(painter)     painter.draw(area, _gui->getRenderer());
#define DRAW_BOX     painters.box.draw(area, _gui->getRenderer());
#define DRAW_FRAME   painters.frame.draw(area, _gui->getRenderer());
#define DRAW_DESIGN  painters.design.drawDesign(area, designPush, _gui->getRenderer());

namespace GRO
{
    
    

Checkbox::Checkbox()
        : Widget(CHECKBOX, Dimensions(20, 20))
{
    PainterMap* painterMap = getPainterMap();
    if(painterMap != NULL)
    {
        painters.box = painterMap->Default_LightBoxPainter;
        painters.frame = painterMap->Default_ButtonDownPainter;
        painters.design = painterMap->Default_CheckDesignPainter;
    }
    
    designPush = new Rect(6, 6, 6, 6);
}
Checkbox::Checkbox(const Dimensions& dims)
        : Widget(CHECKBOX, dims)
{
    PainterMap* painterMap = getPainterMap();
    if(painterMap != NULL)
    {
        painters.box = painterMap->Default_LightBoxPainter;
        painters.frame = painterMap->Default_ButtonDownPainter;
        painters.design = painterMap->Default_CheckDesignPainter;
    }
    
    designPush = new Rect(6, 6, 6, 6);
}
Checkbox::Checkbox(const Dimensions& dims, bool value)
        : Widget(CHECKBOX, dims)
{
    PainterMap* painterMap = getPainterMap();
    if(painterMap != NULL)
    {
        painters.box = painterMap->Default_LightBoxPainter;
        painters.frame = painterMap->Default_ButtonDownPainter;
        painters.design = painterMap->Default_CheckDesignPainter;
    }
    
    designPush = new Rect(6, 6, 6, 6);
    
    checked.set(value);
}
Checkbox::Checkbox(const Dimensions& dims, bool* LinkedVariable)
        : Widget(CHECKBOX, dims)
{
    PainterMap* painterMap = getPainterMap();
    if(painterMap != NULL)
    {
        painters.box = painterMap->Default_LightBoxPainter;
        painters.frame = painterMap->Default_ButtonDownPainter;
        painters.design = painterMap->Default_CheckDesignPainter;
    }
    
    designPush = new Rect(6, 6, 6, 6);
    
    checked.link(LinkedVariable);
}

Checkbox::~Checkbox()
{}

void Checkbox::link(bool* LinkedVariable)
{
    checked.link(LinkedVariable);
}

void Checkbox::set(bool Checked)
{
    checked.set(Checked);
}
void Checkbox::check()
{
    checked.set(true);
}
void Checkbox::uncheck()
{
    checked.set(false);
}

void Checkbox::draw(SDL_Rect* clipRect)
{
    if(_folded || _hidden || _gui == NULL || _gui->getRenderer() == NULL)
        return;
    
    SDL_Rect area = MakeRect(pos.x, pos.y, dims.w, dims.h);
    DRAW_BOX;

    if(checked.get())
    {
        DRAW_DESIGN;
    }

    DRAW_FRAME;
}

Widget* Checkbox::findWidget(int mx, int my)
{
    if(_folded || _hidden)
        return NULL;
    if (!InRect(mx, my, pos, dims))
        return NULL;
    return this;
}

Widget* Checkbox::action(PointerCursor* mouse, ActionEnum actionType)
{
    if(_folded || _hidden || mouse == NULL)
        return NULL;
    if (InRect(mouse->mx, mouse->my, pos, dims))
    {
        if(actionType == ACTION_ACTIVATE)
            checked.set(!checked.get());
        return this;
    }
    return NULL;
}
Widget* Checkbox::action(NavCursor* nav, ActionEnum actionType)
{
    if(_folded || _hidden || nav == NULL)
        return NULL;
    if(actionType == ACTION_ACTIVATE)
    {
        if(actionType == ACTION_ACTIVATE)
            checked.set(!checked.get());
    }
    return this;
}




    

TiXmlElement* Checkbox::save(TiXmlElement* parent)
{
    TiXmlElement* w = new TiXmlElement("Checkbox");
    parent->LinkEndChild(w);
    
    saveWidgetAttributes(w);
    
    w->SetAttribute("checked", checked.get());
    
    return w;
}


bool getBool(const std::string& s);
Beacon* getBeaconByName(Widget* w, const std::string& name);

void Checkbox::connectBeacon(Widget* widget, Beacon* beacon, const std::string& fn_name, const std::vector<std::string>& args)
{
    Widget* w = widget;
    if(w == NULL)
        return;
    
    Beacon* b = beacon;
    if(b == NULL)
        return;
    
    if(fn_name == "check" && args.size() == 0)
    {
        b->connect<Checkbox, void>(this, &Checkbox::check);
    }
    else if(fn_name == "uncheck" && args.size() == 0)
    {
        b->connect<Checkbox, void>(this, &Checkbox::uncheck);
    }
    else if(fn_name == "set" && args.size() == 1)
    {
        b->connect<Checkbox, void, bool>(this, &Checkbox::set, getBool(args[0]));
    }
    else
        Widget::connectBeacon(widget, beacon, fn_name, args);
    return;
}
    
    
    
    
    
    
    
}
